const UserService = require('../../service/admin/UserService')
const JWT = require('../../utils/jwt')

const UserController = {
    login: async (req, res) => {
        const { username, password } = req.body
        let result = await UserService.login(username, password)

        if (result.length === 0) {
            res.send({
                code: -1,
                msg: '用户名密码不匹配'
            })
        }
        else {
            const token = JWT.generate({ username: username }, '1d')
            res.header('Authorization', token)
            console.log(result);
            res.send({
                code: 200,
                msg: '登录成功',
                data: {
                    username: result[0].username,
                    _id: result[0]._id,
                    role: result[0].role,
                    gender: result[0].gender ?? 0,
                    introduction: result[0].introduction ?? '暂无描述',
                    avatar: result[0].avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
                }
            })
        }
    },
    upload: async (req, res) => {
        // console.log("req.body");
        // console.log(req.body);
        // console.log("req.file");
        // console.log(req.file);

        const userData = {
            ...req.body,
            avatar: req.file ? `/avataruploads/${req.file.filename}` : ''
        }
        await UserService.upload(userData)

        const getData = async () => {
            const result = await UserService.getAvatar(userData._id)
            const temp = {
                ...userData,
                avatar: result[0].avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
            }
            console.log("没有传文件时的返回值");
            console.log(temp);
            return temp
        }
        if (req.file) {
            res.send({
                code: 200,
                msg: '修改信息成功',
                data: {
                    userData
                }
            })
        } else {
            getData().then(val => {
                return val
            }).then(val => {
                res.send({
                    code: 200,
                    msg: '修改信息成功',
                    data: {
                        userData: val
                    }
                })
            })

        }

    },
    userAdd: async (req, res) => {
        console.log("userAdd");
        console.log(req.body);
        console.log(req.file);
        const result = await UserService.userCheck(req.body.username)
        if (result.length === 0) {
            const data = {
                ...req.body
            }
            if (req.file) {
                data.avatar = `/avataruploads/${req.file.filename}`
            } else {
                data.avatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
            }
            console.log("data");
            console.log(data);
            console.log("------------");
            await UserService.userAdd(data)
            res.send({
                code: 200,
                msg: '添加成功'
            })
        } else {
            res.send({
                code: -1,
                msg: '用户名已存在'
            })
        }
    },
    getList: async (req, res) => {
        const result = await UserService.getList()
        res.send({
            code: 200,
            msg: '获取用户列表成功',
            data: result
        })
    },
    userDelete: async (req, res) => {
        await UserService.userDelete(req.params.id)
        res.send({
            code: 200,
            msg: '删除成功'
        })
    },
    userEdit: async (req, res) => {
        await UserService.userEdit(req.body)
        res.send({
            code: 200,
            msg: '修改成功'
        })
    }
}


module.exports = UserController